package account

import (
  "encoding/json"
  "fmt"
  "gitee.com/yizhisamoye/GoWeChat/entity/common"
  "gitee.com/yizhisamoye/GoWeChat/utils"
)

/**
评论
*/

//开启评论
func(w WxAccount)OpenComment(msgDataId int,index ...int)error{
  url:=fmt.Sprintf(BaseUrl+"/cgi-bin/comment/open?access_token=%s",w.accessToken)
  data:=map[string]int{
    "msg_data_id":msgDataId,
  }
  if len(index)>0{
    data["index"]=index[0]
  }
  _,err:=utils.SendJson(url,data)
  return err
}
//关闭评论
func(w WxAccount)CloseComment(msgDataId int,index ...int)error{
  url:=fmt.Sprintf(BaseUrl+"/cgi-bin/comment/close?access_token=%s",w.accessToken)
  data:=map[string]int{
    "msg_data_id":msgDataId,
  }
  if len(index)>0{
    data["index"]=index[0]
  }
  _,err:=utils.SendJson(url,data)
  return err
}
//获取评论
//index从0开始
//type=0 普通评论&精选评论 type=1 普通评论 type=2 精选评论
func(w WxAccount)GetComment(msgDataId,index,begin,count,commentType int)(int,[]common.Comment,error){
  url:=fmt.Sprintf(BaseUrl+"/cgi-bin/comment/list?access_token=%s",w.accessToken)
  result,err:=utils.SendJson(url,map[string]int{
    "msg_data_id":msgDataId,
    "index":index,
    "begin":begin,
    "count":count,
    "type":commentType,
  })
  if err!=nil{
    return 0,nil,err
  }
  commentBytes,_:=json.Marshal(result["comment"])
  comments:=make([]common.Comment,0)
  json.Unmarshal(commentBytes,comments)
  return int(result["total"].(float64)),comments,nil
}
//将评论标记为精选
func(w WxAccount)RecommendComment(msgDataId,index,userCommentId int)error{
  url:=fmt.Sprintf(BaseUrl+"/cgi-bin/comment/markelect?access_token=%s",w.accessToken)
  _,err:=utils.SendJson(url,map[string]int{
    "msg_data_id":msgDataId,
    "index":index,
    "user_comment_id":userCommentId,
  })
  return err
}
//取消评论精选
func(w WxAccount)UnRecommendComment(msgDataId,index,userCommentId int)error{
  url:=fmt.Sprintf(BaseUrl+"/cgi-bin/comment/unmarkelect?access_token=%s",w.accessToken)
  _,err:=utils.SendJson(url,map[string]int{
    "msg_data_id":msgDataId,
    "index":index,
    "user_comment_id":userCommentId,
  })
  return err
}
//删除评论
func(w WxAccount)DeleteComment(msgDataId,index,userCommentId int)error{
  url:=fmt.Sprintf(BaseUrl+"/cgi-bin/comment/delete?access_token=%s",w.accessToken)
  _,err:=utils.SendJson(url,map[string]int{
    "msg_data_id":msgDataId,
    "index":index,
    "user_comment_id":userCommentId,
  })
  return err
}
//回复评论
func(w WxAccount)ReplyComment(msgDataId,index,userCommentId int,content string)error{
  url:=fmt.Sprintf(BaseUrl+"/cgi-bin/comment/reply/add?access_token=%s",w.accessToken)
  _,err:=utils.SendJson(url,map[string]interface{}{
    "msg_data_id":msgDataId,
    "index":index,
    "user_comment_id":userCommentId,
    "content":content,
  })
  return err
}
//删除回复
func(w WxAccount)DeleteReply(msgDataId,index,userCommentId int,content string)error{
  url:=fmt.Sprintf(BaseUrl+"/cgi-bin/comment/reply/delete?access_token=%s",w.accessToken)
  _,err:=utils.SendJson(url,map[string]interface{}{
    "msg_data_id":msgDataId,
    "index":index,
    "user_comment_id":userCommentId,
    "content":content,
  })
  return err
}
